package com.uit.httt04.duongvanlam.facedetection.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class TrainingXML extends Object
{
	private List<AlgorithmXML> listAlgorithm;

	public List<AlgorithmXML> getListAlgorithm()
	{
		return listAlgorithm;
	}

	public void setListAlgorithm(List<AlgorithmXML> Training)
	{
		this.listAlgorithm = Training;
	}

	public static void WriteToXmlFile(TrainingXML trainingXML, String fileName, String folderPath)
	{
		try
		{
			File folder = new File(folderPath);
			if (folder.exists() == false)
				folder.mkdirs();

			File file = new File(folder, fileName);
			if (file.exists() == false)
				file.createNewFile();

			FileOutputStream fileOutputStram = new FileOutputStream(file);

			XmlSerializer serializer = Xml.newSerializer();

			serializer.setOutput(fileOutputStram, "UTF-8");
			//
			//
			//
			serializer.startDocument("UTF-8", true);
			// <Training>
			serializer.startTag("", TAGXML.Training);

			for (int i = 0; i < trainingXML.getListAlgorithm().size(); i++)
			{
				AlgorithmXML algorithmXML = trainingXML.getListAlgorithm().get(i);
				// <Algorithm>
				serializer.startTag("", TAGXML.Algorithm);
				// <Name>
				serializer.startTag("", TAGXML.Name);
				serializer.text(algorithmXML.getAlgorithmName()); //
				serializer.endTag("", TAGXML.Name);
				// </Name>
				// <TimestampStarting>
				serializer.startTag("", TAGXML.TimestampStarting);
				serializer.text(String.valueOf(algorithmXML.getTimestampStarting()));//
				serializer.endTag("", TAGXML.TimestampStarting);
				// </timestampStarting>
				// <TrainingLengthByMilliseconds>
				serializer.startTag("", TAGXML.TrainingLengthByMilliseconds);
				serializer.text(String.valueOf(algorithmXML.getTrainingLengthByMilliseconds()));//
				serializer.endTag("", TAGXML.TrainingLengthByMilliseconds);
				// </TrainingLengthByMilliseconds>
				// <TrainingFolderPath>
				serializer.startTag("", TAGXML.TrainingFolderPath);
				serializer.text(algorithmXML.getTrainingFolderPath());//
				serializer.endTag("", TAGXML.TrainingFolderPath);
				// </TrainingFolderPath>
				// <ImageHeight>
				serializer.startTag("", TAGXML.ImageHeight);
				serializer.text(String.valueOf(algorithmXML.getImageHeight()));//
				serializer.endTag("", TAGXML.ImageHeight);
				// </ImageHeight>
				// <ImageWidth>
				serializer.startTag("", TAGXML.ImageWidth);
				serializer.text(String.valueOf(algorithmXML.getImageWidth()));//
				serializer.endTag("", TAGXML.ImageWidth);
				// </ImageWidth>
				// <TotalAmountOfImages>
				serializer.startTag("", TAGXML.TotalAmountOfImages);
				serializer.text(String.valueOf(algorithmXML.getTotalAmountOfImages()));//
				serializer.endTag("", TAGXML.TotalAmountOfImages);
				// </TotalAmountOfImages>
				// <TotalAmountOfFaces>
				serializer.startTag("", TAGXML.TotalAmountOfFaces);
				serializer.text(String.valueOf(algorithmXML.getTotalAmountOfFaces()));//
				serializer.endTag("", TAGXML.TotalAmountOfFaces);
				// </TotalAmountOfFaces>
				// <Objects>
				serializer.startTag("", TAGXML.Objects);
				for (int m = 0; m < algorithmXML.getObjects().size(); m++)
				{
					// <Object>
					serializer.startTag("", TAGXML.Object);
					// <PersonName>
					serializer.startTag("", TAGXML.PersonName);
					serializer.text(algorithmXML.getObjects().get(m).getPersonName());//
					serializer.endTag("", TAGXML.PersonName);
					// </PersonName>
					// <ImagePath>
					serializer.startTag("", TAGXML.ImagePath);
					serializer.text(algorithmXML.getObjects().get(m).getImagePath());//
					serializer.endTag("", TAGXML.ImagePath);
					// </ImagePath>
					serializer.endTag("", TAGXML.Object);
					// </Object>
				}
				serializer.endTag("", TAGXML.Objects);//
				// </Objects>
				// <EigenvectorMaxN>
				serializer.startTag("", TAGXML.EigenvectorMaxN);
				serializer.text(String.valueOf(algorithmXML.getEigenvectorMaxN()));
				serializer.endTag("", TAGXML.EigenvectorMaxN);
				// </EigenvectorMaxN>
				// <MatrixDataSplitBy>
				serializer.startTag("", TAGXML.MatrixDataSplitBy);
				serializer.text(String.valueOf(algorithmXML.getMatrixDataSplitBy()));
				serializer.endTag("", TAGXML.MatrixDataSplitBy);
				// </MatrixDataSplitBy>
				// <EigenvectorMatrix>
				serializer.startTag("", TAGXML.EigenvectorMatrix);
				// <MatrixHeight>
				serializer.startTag("", TAGXML.MatrixHeight);
				serializer.text(String.valueOf(algorithmXML.getEigenvectorMatrix().getHeight()));
				serializer.endTag("", TAGXML.MatrixHeight);
				// </MatrixHeight>
				// <MatrixWidth>
				serializer.startTag("", TAGXML.MatrixWidth);
				serializer.text(String.valueOf(algorithmXML.getEigenvectorMatrix().getWidth()));
				serializer.endTag("", TAGXML.MatrixWidth);
				// </MatrixWidth>
				// <MatrixData>
				serializer.startTag("", TAGXML.MatrixData);
				serializer.text(String.valueOf(algorithmXML.getEigenvectorMatrix().getData()));
				serializer.endTag("", TAGXML.MatrixData);
				// </MatrixData>
				serializer.endTag("", TAGXML.EigenvectorMatrix);
				// </EigenvectorMatrix>
				// <EigenvalueMatrix>
				serializer.startTag("", TAGXML.EigenvalueMatrix);
				// <MatrixHeight>
				serializer.startTag("", TAGXML.MatrixHeight);
				serializer.text(String.valueOf(algorithmXML.getEigenvalueMatrix().getHeight()));
				serializer.endTag("", TAGXML.MatrixHeight);
				// </MatrixHeight>
				// <MatrixWidth>
				serializer.startTag("", TAGXML.MatrixWidth);
				serializer.text(String.valueOf(algorithmXML.getEigenvalueMatrix().getWidth()));
				serializer.endTag("", TAGXML.MatrixWidth);
				// </MatrixWidth>
				// <MatrixData>
				serializer.startTag("", TAGXML.MatrixData);
				serializer.text(String.valueOf(algorithmXML.getEigenvalueMatrix().getData()));
				serializer.endTag("", TAGXML.MatrixData);
				// </MatrixData>
				serializer.endTag("", TAGXML.EigenvalueMatrix);
				// </EigenvalueMatrix>
				// <ProjectedObjects>
				serializer.startTag("", TAGXML.ProjectedObjects);
				for (int m = 0; m < algorithmXML.getProjectedObjects().size(); m++)
				{
					// <ProjectedObject>
					serializer.startTag("", TAGXML.ProjectedObject);
					// <PersonName>
					serializer.startTag("", TAGXML.PersonName);
					serializer.text(algorithmXML.getProjectedObjects().get(m).getPersonName());
					serializer.endTag("", TAGXML.PersonName);
					// </PersonName>
					// <ImagePath>
					serializer.startTag("", TAGXML.ImagePath);
					serializer.text(algorithmXML.getProjectedObjects().get(m).getImagePath());
					serializer.endTag("", TAGXML.ImagePath);
					// </ImagePath>
					// <ProjectedObjectMatrix>
					serializer.startTag("", TAGXML.ProjectedObjectMatrix);
					// <MatrixHeight>
					serializer.startTag("", TAGXML.MatrixHeight);
					serializer.text(String.valueOf(algorithmXML.getProjectedObjects().get(m).getMatrix().getHeight()));
					serializer.endTag("", TAGXML.MatrixHeight);
					// </MatrixHeight>
					// <MatrixWidth>
					serializer.startTag("", TAGXML.MatrixWidth);
					serializer.text(String.valueOf(algorithmXML.getProjectedObjects().get(m).getMatrix().getWidth()));
					serializer.endTag("", TAGXML.MatrixWidth);
					// </MatrixWidth>
					// <MatrixData>
					serializer.startTag("", TAGXML.MatrixData);
					serializer.text(algorithmXML.getProjectedObjects().get(m).getMatrix().getData());
					serializer.endTag("", TAGXML.MatrixData);
					// </MatrixData>
					serializer.endTag("", TAGXML.ProjectedObjectMatrix);
					// </ProjectedObjectMatrix>
					serializer.endTag("", TAGXML.ProjectedObject);
					// </ProjectedObject>
				}
				serializer.endTag("", TAGXML.ProjectedObjects);
				// </ProjectedObjects>
				// <AutoID>
				serializer.startTag("", TAGXML.AutoID);
				for (int m = 0; m < algorithmXML.getAutoID().size(); m++)
				{
					// <IDToName>
					serializer.startTag("", TAGXML.IDToName);
					// <ID>
					serializer.startTag("", TAGXML.ID);
					serializer.text(String.valueOf(algorithmXML.getAutoID().get(m).getID()));
					serializer.endTag("", TAGXML.ID);
					// </ID>
					// <Name>
					serializer.startTag("", TAGXML.Name);
					serializer.text(String.valueOf(algorithmXML.getAutoID().get(m).getName()));
					serializer.endTag("", TAGXML.Name);
					// </Name>
					serializer.endTag("", TAGXML.IDToName);
					// </IDToName>
				}
				serializer.endTag("", TAGXML.AutoID);
				// </AutoID>
				// <AllowedExt>
				serializer.startTag("", TAGXML.AllowedExt);
				for (int m = 0; m < algorithmXML.getAllowedExt().size(); m++)
				{
					// <Ext>
					serializer.startTag("", TAGXML.Ext);
					serializer.text(String.valueOf(algorithmXML.getAllowedExt().get(m)));
					serializer.endTag("", TAGXML.Ext);
					// </Ext>
				}
				serializer.endTag("", TAGXML.AllowedExt);
				// </AllowedExt>
				serializer.endTag("", TAGXML.Algorithm);
				// </Algorithm>
			}
			serializer.endTag("", TAGXML.Training);
			serializer.endDocument();
			//
			//
			//
			serializer.flush();
			fileOutputStram.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	@Override
	public void finalize() throws Throwable
	{
		super.finalize();
	}
	//
}
